import requests

def get_ip_info():
    try:
        # 发送 GET 请求到 ipapi.co/json
        response = requests.get('https://ipapi.co/json')
        
        # 检查请求是否成功
        response.raise_for_status()  # 如果请求失败，将引发 HTTPError
        
        # 解析 JSON 响应
        ip_info = response.json()
        
        # 打印获取到的 IP 信息
        print("IP 信息:")
        print(f"IP: {ip_info.get('ip')}")
        print(f"城市: {ip_info.get('city')}")
        print(f"国家: {ip_info.get('country_name')}")
        print(f"地区: {ip_info.get('region')}")
        print(f"邮政编码: {ip_info.get('postal')}")
        print(f"纬度: {ip_info.get('latitude')}")
        print(f"经度: {ip_info.get('longitude')}")
        print(f"时区: {ip_info.get('timezone')}")
        
    except requests.exceptions.RequestException as e:
        print(f"请求失败: {e}")

if __name__ == "__main__":
    get_ip_info()